package com.org.service.impl;

import com.org.constant.ResponseEnum;
import com.org.exception.RespException;
import com.org.mapper.AgreementDetailMapper;
import com.org.model.AgreementDetail;
import com.org.service.AgreementDetailService;
import com.org.service.facotry.AgreementService;
import com.org.service.facotry.AgreementFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * @author: vane.Q
 * @date: 2024/1/21 12:38
 */
@Repository
public class AgreementDetailServiceImpl implements AgreementDetailService {

    @Autowired
    private AgreementDetailMapper agreementDetailMapper;

    @Autowired
    private AgreementFactory agreementFactory;

    @Override
    public void save(AgreementDetail agreementDetail) {
        agreementDetail.setCreatedAt(new Date());
        agreementDetailMapper.save(agreementDetail);
    }

    @Override
    public ModelAndView getAgreement(String agreementNo, Integer version) {
        AgreementDetail agreementDetail = agreementDetailMapper.queryByAgreementNo(agreementNo);
        if (agreementDetail == null) {
            throw new RespException(ResponseEnum.NO_AGREEMENT_ERROR);
        }
        AgreementService agreementService = agreementFactory.getAgreementDetail(agreementDetail.getAgreementType());
        return agreementService.getAgreement(agreementDetail);
    }

}
